package com.daxt.controller.system;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.sys.FunctionMapper;
import com.daxt.model.dic.MenuType;
import com.daxt.model.dic.MethodType;
import com.daxt.model.service.base.vo.Function;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "功能")
@RestController
public class FunctionController {

		
	@Autowired
	private FunctionMapper functionMapper;
	
//	@ApiOperation(value = "获取接口", notes = "") 
//	@GetMapping("/function/interfaces") 
//	public Result<PageInfo<Function>> interfaces(
//			@ApiParam("") @RequestParam(value = "pageNum", required = false)Integer pageNum,
//			@ApiParam("") @RequestParam(value = "pageSize", required = false)Integer pageSize,
//			@ApiParam("") @RequestParam(value = "interfaceName", required = false)String interfaceName){
		//return functionApi.interfaces(pageNum, pageSize,interfaceName);
//	}
	
	/*@ApiOperation(value = "获取菜单", notes = "") 
	@GetMapping("/function/menus") 
	public Result<List<Function>> menus(){
		return functionApi.menus();
	}*/
	
	
	@ApiOperation(value = "添加", notes = "") 
	@PostMapping("/function") 
	public Result<Function> save(@RequestBody Function entity){
		/*if(!StringUtils.isEmpty(entity.getMenuType())&&MenuType.hasKey(entity.getMenuType())&&!StringUtils.isEmpty(entity.getName())) {
			if(MenuType.MENU.name().equals(entity.getMenuType())) {
				//if(StringUtils.isEmpty(entity.getIcon())) {
				//	return ResultUtil.data(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMes(), null);
				//}
				//entity.setMethod(null);
			}else if(MenuType.INTERFACE.name().equals(entity.getMenuType())) {
				if(StringUtils.isEmpty(entity.getRouter())||StringUtils.isEmpty(entity.getMenuType())||MethodType.hasKey(entity.getMenuType())) {
					return ResultUtil.data(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMes(), null);
				}
				entity.setParentId(null);
				entity.setSort(null);
				entity.setIcon(null);
				entity.setHidden(null);
				entity.setRedirect(null);
				entity.setComponent(null);
				
			}else if(MenuType.FUNCTION.name().equals(entity.getMenuType())){
				entity.setSort(null);
				entity.setIcon(null);
				entity.setHidden(null);
				entity.setRedirect(null);
				entity.setComponent(null);
			}else {
				return ResultUtil.data(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMes(), null);
			}
			QueryWrapper<Function> queryWrapper=new QueryWrapper<Function>();
			queryWrapper.eq("name", entity.getName());
			queryWrapper.or();
			queryWrapper.eq("router", entity.getRouter());
			List<Function> selectList = functionMapper.selectList(queryWrapper);
			if(selectList!=null&&selectList.size()>0) {
				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"资源名或路由不能重复" , null);
			}
			int rows = functionMapper.insert(entity);
			return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
		}else {
			return  ResultUtil.data(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMes(), null);
		}*/
		
		if(StringUtils.isEmpty(entity.getClientType())) {
			entity.setClientType("ADMIN");
		}
		if(StringUtils.isEmpty(entity.getState())) {
			entity.setState("1");
		}
		if(entity.getSort()==null) {
			entity.setSort(1);
		}
		
		
		QueryWrapper<Function> queryWrapper=new QueryWrapper<Function>();
		queryWrapper.eq("funValue", entity.getFunValue());
		List<Function> selectList = functionMapper.selectList(queryWrapper);
		if(selectList!=null&&selectList.size()>0) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "功能不能重复添加！！！", null);
		}
		int rows = functionMapper.insert(entity);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}
	
	
	@ApiOperation(value = "修改", notes = "") 
	@PutMapping("/function") 
	public Result<Function> edit(@RequestBody Function entity){
		/*if(!StringUtils.isEmpty(entity.getMenuType())&&MenuType.hasKey(entity.getMenuType())&&!StringUtils.isEmpty(entity.getName())) {
			if(MenuType.MENU.name().equals(entity.getMenuType())) {
				//if(StringUtils.isEmpty(entity.getIcon())) {
				//	return ResultUtil.data(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMes(), null);
				//}
				entity.setMethod(null);
			}else if(MenuType.INTERFACE.name().equals(entity.getMenuType())) {
				if(StringUtils.isEmpty(entity.getRouter())||StringUtils.isEmpty(entity.getMenuType())||MethodType.hasKey(entity.getMenuType())) {
					return ResultUtil.data(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMes(), null);
				}
				entity.setParentId(null);
				entity.setSort(null);
				entity.setIcon(null);
				entity.setHidden(null);
				entity.setRedirect(null);
				entity.setComponent(null);
			}else if(MenuType.FUNCTION.name().equals(entity.getMenuType())){
				entity.setSort(null);
				entity.setIcon(null);
				entity.setHidden(null);
				entity.setRedirect(null);
				entity.setComponent(null);
			}else {
				return ResultUtil.data(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMes(), null);
			}
			QueryWrapper<Function> queryWrapper=new QueryWrapper<Function>();
			queryWrapper.eq("name", entity.getName());
			queryWrapper.or();
			queryWrapper.eq("router", entity.getRouter());
			List<Function> selectList = functionMapper.selectList(queryWrapper);
			if(selectList!=null&&selectList.size()>0) {
				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"资源名或路由不能重复" , null);
			}
			int rows = functionMapper.updateById(entity);
			return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
		}else {
			return  ResultUtil.data(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMes(), null);
		}*/
		if(StringUtils.isEmpty(entity.getClientType())) {
			entity.setClientType("ADMIN");
		}
		if(StringUtils.isEmpty(entity.getState())) {
			entity.setState("1");
		}
		if(entity.getSort()==null) {
			entity.setSort(1);
		}
		
		
		QueryWrapper<Function> queryWrapper=new QueryWrapper<Function>();
		queryWrapper.eq("funValue", entity.getFunValue());
		List<Function> selectList = functionMapper.selectList(queryWrapper);
		/*if(selectList!=null&&selectList.size()>1) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "修改的功能不能与当前现有功能重复！！！", null);
		}*/
		int rows = functionMapper.updateById(entity);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}
	
	@ApiOperation(value = "删除", notes = "") 
	@DeleteMapping("/function/{id}") 
	public Result<String> del(@PathVariable("id")String id){
		int rows = functionMapper.deleteById(id);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}
	
	
}
